package com.partaljevic.gambling.beans;

import com.partaljevic.gambling.Customer;
import com.partaljevic.gambling.Identifiable;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Ticket implements Serializable {

    @Basic
    private double gain;
    @Basic
    private double billing;
    @Basic
    private double totalQuotes = 1;
    @ManyToOne(targetEntity=CustomerBean.class)
    private Customer customer;
    @Id
    @GeneratedValue
    private Long id;
    @Basic
    private boolean paid;
    @Basic
    private String createdDate;
    @OneToMany(targetEntity=Pair.class, mappedBy="ticket", fetch=FetchType.LAZY)
    private List<Pair> pairsOfTicket;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public boolean isWinner() {
        for (Pair match : pairsOfTicket) {
            if(!match.isWinner())
                return false;
        }
        return true;
    }

    public Ticket() {
    }

    public void setId(long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public double getGain() {
        return gain;
    }

    public void setGain(double gain) {
        this.gain = gain;
    }

    public double getBilling() {
        return billing;
    }

    public void setBilling(double billing) {
        this.billing = billing;
    }

    public double getTotalQuotes() {
        return totalQuotes;
    }

    public void setTotalQuotes(double totalQuotes) {
        this.totalQuotes = totalQuotes;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public List<Pair> getPairsOfTicket() {
        return pairsOfTicket;
    }

    public void setPairsOfTicket(List<Pair> pairsOfTicket) {
        this.pairsOfTicket = pairsOfTicket;
    }

    public void addMatch(Pair match) {
        pairsOfTicket.add(match);
    }

    public Ticket(List<Pair> pairs, double billing) {

        pairsOfTicket = new LinkedList<Pair>();
        for (Pair par : pairs) {
            pairsOfTicket.add(par);
        }
        this.billing = billing;
        for (Pair par : pairsOfTicket) {
            totalQuotes *= par.getQuote();
        }
        gain = this.billing * totalQuotes * 100;
        int a = (int) gain;
        gain = a / 100;
        createdDate = new Date().toString();
        setPaid(false);
    }

    public String show() {
        String downstream = "";
        for (Pair par : pairsOfTicket) {
            downstream += par + "\n";
        }
        downstream += "Gain: " + this.gain + "\n" + this.createdDate + "\n";
        return downstream;
    }

    @Override
    public String toString() {
        return show();
    }

    @Override
    public boolean equals(Object obj) {
        Ticket t = (Ticket) obj;
        if(t.getId()==this.id)return true;
        return false;
    }

    /**
     * @return the paid
     */
    public boolean isPaid() {
        return paid;
    }

    /**
     * @param paid the paid to set
     */
    public void setPaid(boolean paid) {
        this.paid = paid;
    }


}
